package com.puyuba.puyuba.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.view.fragment.MessageFindStudentFragment;
import com.puyuba.puyuba.view.fragment.MessageFindTeacherFragment;
import com.puyuba.puyuba.view.fragment.MessageFragment;
import com.puyuba.puyuba.view.fragment.MessageOneFragment;
import com.puyuba.puyuba.view.fragment.MessageTwoFragment;
import com.puyuba.puyuba.view.fragment.TeacherCourseFragment;
import com.puyuba.puyuba.view.fragment.TeacherWorkFragment;
import com.puyuba.puyuba.view.viewpager.WrapViewPager;
import com.pyb.java.basemodule.BaseActivity;
import com.pyb.java.commonutil.ImageHelper;

import java.util.ArrayList;

/**
 *教师个人主页
 */
public class TeacherHomePageActivity extends BaseActivity {
    
    private ImageView ivBack;
    private RelativeLayout rlImage;
    private ImageView ivTeacher;
    private TextView tvTitle;
    private TextView tvCountZan;
    private TextView tvCountFan;
    private RelativeLayout rlFocus;
    private TextView tvIsfocus;
    private ImageView ivArrow;
    private SlidingTabLayout tl3;
    private WrapViewPager vpTeacherPage;
    private boolean isfocus;
    private String[] mTitles ;
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_teacher_homepage;
    }

    public static void startActivity(Context context,int id) {
        Intent intent = new Intent(context, TeacherHomePageActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        rlImage = (RelativeLayout) findViewById(R.id.rl_image);
        ivTeacher = (ImageView) findViewById(R.id.iv_teacher);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvCountZan = (TextView) findViewById(R.id.tv_count_zan);
        tvCountFan = (TextView) findViewById(R.id.tv_count_fan);
        rlFocus = (RelativeLayout) findViewById(R.id.rl_focus);
        tvIsfocus = (TextView) findViewById(R.id.tv_isfocus);
        ivArrow = (ImageView) findViewById(R.id.iv_arrow);
        tl3 = (SlidingTabLayout) findViewById(R.id.tl_3);
        vpTeacherPage = (WrapViewPager) findViewById(R.id.vp_teacher_page);

        int id = getIntent().getIntExtra("id", 0);

        initData();
        initListener();
        initFragments();
        initTab();
    }

    private void initData() {
        ImageHelper.getInstance().displayWithCorner(ivTeacher,"",R.mipmap.default_avatar);
    }

    private void initListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        rlFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isfocus){
                    rlFocus.setBackground(ContextCompat.getDrawable(TeacherHomePageActivity.this,R.drawable.bg_ff523b_dp9));
                    tvIsfocus.setText("关注");
                    tvIsfocus.setTextColor(ContextCompat.getColor(TeacherHomePageActivity.this,R.color.white));
                }else {
                    rlFocus.setBackground(ContextCompat.getDrawable(TeacherHomePageActivity.this,R.drawable.shape_line_e65345_dp9));
                    tvIsfocus.setText("已关注");
                    tvIsfocus.setTextColor(ContextCompat.getColor(TeacherHomePageActivity.this,R.color.color_F66B38));
                }
                isfocus = !isfocus;
            }
        });
    }
    private void initFragments() {
        mFragments.clear();
        mFragments.add(TeacherWorkFragment.getInstance(1));
        mFragments.add(TeacherCourseFragment.getInstance(1));
        mTitles = new String[]{"作品 0", "课程 0"};

    }

    private void initTab() {
        vpTeacherPage.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        vpTeacherPage.setOffscreenPageLimit(2);
        tl3.setViewPager(vpTeacherPage);
        tl3.setOnTabSelectListener(new com.flyco.tablayout.listener.OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vpTeacherPage.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        vpTeacherPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

            @Override
            public void onPageSelected(int position) {
                tl3.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
        vpTeacherPage.setCurrentItem(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

}
